/**
 * 
 */

var SEARCH_TYPE_CUSTOMER 		= 0;
var SEARCH_TYPE_MANAGER			= 1;
var SEARCH_TYPE_OPERATOR		= 2;
var SEARCH_TYPE_REPAIR_VENDOR	= 3;

var BIZ_TYPE_CUSTOMER		= "001";
var BIZ_TYPE_SUPPLIER		= "002";
var BIZ_TYPE_REPAIR_VENDOR	= "003";

var dhxWins;
var w1;
var custForm;
var custGrid;

function search(searchType, upCustId, useYn, custType, word) {
	
	var bizType = null;
	if ( searchType == SEARCH_TYPE_CUSTOMER ) {
		bizType = BIZ_TYPE_CUSTOMER;
	}
	else if ( searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
		bizType = BIZ_TYPE_REPAIR_VENDOR;
	}
	else {
		bizType = BIZ_TYPE_SUPPLIER;
	}
	
	$.ajax({
		url: contextPath + "/customer/list",
		data: {bizType:bizType, upCustId:upCustId, useYn:useYn, custType:custType, word:word},
		type: 'GET',
		dataType: 'json'
	}).done(function(data) {
		custGrid.clearAll();
		custGrid.parse(data, "js");
	})
};

function searchCustomer() {
	var useYn = custForm.getItemValue("use_yn")
	
	if ( useYn.length == 0 ) {
		useYn = 'Y';
	}

	var custType = custForm.getItemValue("customer_type");
	var word = custForm.getItemValue("search_word");
	
	search(SEARCH_TYPE_CUSTOMER, null, useYn, custType, word);
}

function searchManager() {
	var useYn = custForm.getItemValue("use_yn")
	
	if ( useYn.length == 0 ) {
		useYn = 'Y';
	}

	var custId = custForm.getItemValue("cust_id");
	var custType = custForm.getItemValue("customer_type");
	var word = custForm.getItemValue("search_word");
	
	search(SEARCH_TYPE_MANAGER, custId, useYn, custType, word);

}

function searchOperator() {
	var useYn = custForm.getItemValue("use_yn")
	
	if ( useYn.length == 0 ) {
		useYn = 'Y';
	}

	var custId = custForm.getItemValue("cust_id");
	var custType = custForm.getItemValue("customer_type");
	var word = custForm.getItemValue("search_word");
	
	search(SEARCH_TYPE_OPERATOR, custId, useYn, custType, word);
}

function searchRepairVendor() {
	var useYn = custForm.getItemValue("use_yn")
	
	if ( useYn.length == 0 ) {
		useYn = 'Y';
	}

	var custId = custForm.getItemValue("cust_id");
	var custType = custForm.getItemValue("customer_type");
	var word = custForm.getItemValue("search_word");
	
	search(SEARCH_TYPE_REPAIR_VENDOR, custId, useYn, custType, word);
}

function loadBizType() {
	$.ajax({
		url: contextPath + '/cm_common/list',
		data: {companyCode:'1000', majorCode:'TMO12'},
		type: 'GET',
		dataType: 'json'
	}).done(function(data) {
		if ( data != null && data.length > 0 ) {
			$("#biz_type").append($('<option>', { value: '', text: '' }));
			
			$.each(data, function(i, o) {
				$("#biz_type").append($('<option>', { value: o['minorCode'], text: o['minorName'] }));
			});
		}
	});
}

function loadUseYn() {
	$.ajax({
		url: contextPath + '/cm_common/list',
		data: {companyCode:'1000', majorCode:'TMO11'},
		type: 'GET',
		dataType: 'json'
	}).done(function(data) {
		if ( data != null && data.length > 0 ) {
			var opts = custForm.getOptions("use_yn");
			opts.add(new Option('', ''));
			
			$.each(data, function(i, o) {
				opts.add(new Option(o['minorName'], o['minorCode']));
			});
		}
	});
}

function loadCustType() {
	$.ajax({
		url: contextPath + '/cm_common/list',
		data: {companyCode:'1000', majorCode:'TMO10'},
		type: 'GET',
		dataType: 'json'
	}).done(function(data) {
		if ( data != null && data.length > 0 ) {
			var opts = custForm.getOptions("customer_type");
			opts.add(new Option('', ''));
			
			$.each(data, function(i, o) {
				opts.add(new Option(o['minorName'], o['minorCode']));
			});
		}
	});
}

function initCustomerPopupUI(searchType, title) {
	dhxWins = new dhtmlXWindows();
	dhxWins.attachViewportTo(parent.document.getElementById("contents_div"));
	
	w1 = dhxWins.createWindow("w1", 20, 30, 800, 500);
	w1.setText(title);
	w1.setModal(true);
	w1.center();
	w1.button("park").hide();
	w1.button("minmax").hide();

	var formData;
	if ( searchType == SEARCH_TYPE_CUSTOMER || searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
		formData = [
	            {type: "settings", position: "label-left", labelWidth: 120, inputWidth: 150, offsetTop:0},
	            {type: "block", width:"100%", 
	            	list:[
	            	      {type: "select", name: "biz_type", label: "사업자 구분", options:[
	            	      ]},
	            	      {type: "newcolumn", offset: 100},
	            	      {type: "select", name: "use_yn", label: "사용여부", options:[
	            	      ]},
	            	]
	            },
	            {type: "block", width:"100%", 
	            	list:[
	            	      {type: "select", name: "customer_type", label: "고객유형분류", options:[
	            	      ]},
	            	      {type: "newcolumn", offset: 100},
	            	      {type: "input", name: "search_word", label: "조회"},
	            	      {type: "newcolumn"},
	            	      {type: "button", name: "search_button", value: "검색", inputHeight:24}
	            	]
	            },
	            {type: "container", name: "result_grid", inputWidth:770, inputHeight:300},
	            {type: "container", name: "result_paging_area", inputWidth:770},
	            {type: "container", name: "result_recinfo_area", inputWidth:770},	            
	            {type: "block", width:"100%",
	            	list: [
	            	       {type: "label", label: ""},
	            	       {type: "newcolumn", offset: 470},
	            	       {type: "button", name: "confirm_button", value: "확인"},
	            	       {type: "newcolumn"},
	            	       {type: "button", name: "close_button", value: "닫기"}
	            	       ]
	            }
	           ];
	}
	else {
		formData = [
		            {type: "settings", position: "label-left", labelWidth: 120, inputWidth: 150},
		            {type: "block", width:"100%", 
		            	list:[
		            	      {type: "input", name: "cust_id", label: "고객", inputWidth: 73, readonly: true},
		            	      {type: "newcolumn"},
		            	      {type: "input", name: "cust_name", inputWidth: 74, readonly: true},
		            	      {type: "newcolumn", offset: 100},
		            	      {type: "select", name: "use_yn", label: "사용여부", options:[
		            	      ]},
		            	]
		            },
		            {type: "block", width:"100%", 
		            	list:[
		            	      {type: "select", name: "customer_type", label: "고객유형분류", options:[
		            	      ]},
		            	      {type: "newcolumn", offset: 100},
		            	      {type: "input", name: "search_word", label: "조회"},
		            	      {type: "newcolumn"},
		            	      {type: "button", name: "search_button", value: "검색", inputHeight:24}
		            	]
		            },
		            {type: "container", name: "result_grid", inputWidth:770, inputHeight:320},
		            {type: "block", width:"100%",
		            	list: [
		            	       {type: "label", label: ""},
		            	       {type: "newcolumn", offset: 470},
		            	       {type: "button", name: "confirm_button", value: "확인"},
		            	       {type: "newcolumn"},
		            	       {type: "button", name: "close_button", value: "취소"}
		            	       ]
		            }
		           ];
	}
	
	custForm = w1.attachForm();
	custForm.loadStruct(formData);
	
	if ( searchType == SEARCH_TYPE_CUSTOMER ) {
		var opts = custForm.getOptions("biz_type");
		opts.add(new Option('고객', '001'));
	}
	else if ( searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
		var opts = custForm.getOptions("biz_type");
		opts.add(new Option('수리처', '003'));
	}
	
	custForm.attachEvent("onButtonClick", function(name) {
		if ( name == "search_button" ) {
			if ( searchType == SEARCH_TYPE_CUSTOMER ) {
				searchCustomer();	
			}
			else if ( searchType == SEARCH_TYPE_MANAGER ) {
				searchManager();	
			}
			else if ( searchType == SEARCH_TYPE_OPERATOR ) {
				searchOperator();	
			}
			else if ( searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
				searchRepairVendor();	
			}
		}
		else if ( name == "confirm_button" ) {
			custGrid.forEachRow(function(rowId) {
				var selectedFlag = custGrid.cells(rowId, 0).getValue();
				if ( selectedFlag == true ) {
					var custName = custGrid.cells(rowId, 1).getValue();	
					var custId = custGrid.cells(rowId, 2).getValue();
					
					if ( searchType == SEARCH_TYPE_CUSTOMER ) {
						mainForm.setItemValue("customer_name", custName);
						mainForm.setItemValue("customer_code", custId);
					}
					else if ( searchType == SEARCH_TYPE_MANAGER ) {
						mainForm.setItemValue("manager_name", custName);
						mainForm.setItemValue("manager_code", custId);
					}
					else if ( searchType == SEARCH_TYPE_OPERATOR ) {
						mainForm.setItemValue("operator_name", custName);
						mainForm.setItemValue("operator_code", custId);
					}
					else if ( searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
						mainForm.setItemValue("repair_vendor_name", custName);
						mainForm.setItemValue("repair_vendor_code", custId);
					}

					w1.close();					
				}
			});
		}
		else if ( name == "close_button" ) {
			w1.close();
		}
	});
	
	custGrid = new dhtmlXGridObject(custForm.getContainer("result_grid"));
	custGrid.setImagePath(contextPath + "/resources/common/dhtmlx/imgs/dhxgrid_terrace/");
	custGrid.setHeader(",고객명,고객코드,대표자명,사업자구분,고객유형분류,사용여부,수리처코드");
	custGrid.setColumnIds(",custName,custAccNo,ceoName,bizTypeName,custTypeName,useYnName,repairCustCode");
	custGrid.setColTypes("ra,txt,txt,txt,txt,txt,txt,txt");
	custGrid.setInitWidths("50,150,130,100,100,100,100,100");
	custGrid.enableEditEvents(false, false, false);
	custGrid.init();
	custGrid.attachEvent("onRowDblClicked", function(row, col) {
		var custName = custGrid.cells(row, 1).getValue();
		var custId = custGrid.cells(row, 2).getValue();
		
		if ( searchType == SEARCH_TYPE_CUSTOMER ) {
			mainForm.setItemValue("customer_name", custName);
			mainForm.setItemValue("customer_code", custId);
		}
		else if ( searchType == SEARCH_TYPE_MANAGER ) {
			mainForm.setItemValue("manager_name", custName);
			mainForm.setItemValue("manager_code", custId);
		}
		else if ( searchType == SEARCH_TYPE_OPERATOR ) {
			mainForm.setItemValue("operator_name", custName);
			mainForm.setItemValue("operator_code", custId);
		}
		else if ( searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
			mainForm.setItemValue("repair_vendor_name", custName);
			mainForm.setItemValue("repair_vendor_code", custId);
		}
		
		w1.close();
	});
}

function showCustomer() {
	initCustomerPopupUI(SEARCH_TYPE_CUSTOMER, "고객 조회");

	//loadBizType();
	loadCustType();
	loadUseYn();
}

function showManager() {
	initCustomerPopupUI(SEARCH_TYPE_MANAGER, "관리국 조회");

	//loadBizType();
	loadCustType();
	loadUseYn();
}

function showOperator() {
	initCustomerPopupUI(SEARCH_TYPE_OPERATOR, "운용국 조회");

	//loadBizType();
	loadCustType();
	loadUseYn();
}

function showRepairVendor() {
	initCustomerPopupUI(SEARCH_TYPE_REPAIR_VENDOR, "수리처 조회");

	//loadBizType();
	loadCustType();
	loadUseYn();
}